#include "rgb_led.h"

Pixel::Pixel()
{
    rgb_mode = 1;
}

Pixel::~Pixel()
{
    
}

void Pixel::init()
{
    FastLED.addLeds<WS2812B, RGB_LED_PIN, GRB>(color_buffers, RGB_LED_NUM);
    FastLED.setBrightness(200);
}

void Pixel::setMode(uint8_t mode)
{
    rgb_mode = mode;
}

Pixel& Pixel::setRGB(int id, int r, int g, int b)
{
    color_buffers[id] = CRGB(r, g, b);
    FastLED.show();

    return *this;
}

void Pixel::setRGB(int x, int y, int r, int g, int b)
{
    if (rgb_mode == 0)
    {
        color_buffers[(7-y)*8+x] = CRGB(r, g, b);
    }
    else if (rgb_mode == 1)
    {
        color_buffers[y*8+(7-x)] = CRGB(r, g, b);
    }
}

Pixel& Pixel::setBrightness(float duty)
{
    duty = constrain(duty, 0, 1);
    FastLED.setBrightness((uint8_t)(255 * duty));
    FastLED.show();

    return *this;
}

// 逐行填充
void Pixel::setScreen(uint8_t *buf)
{
    uint8_t i = 0;

    // Y轴
    for (uint8_t y = 0; y < RGB_LED_NUM_Y; y++)
    {
        // X轴
        for (uint8_t x = 0; x < RGB_LED_NUM_X; x++)
        {
            // 每个像素点占3个byte
            color_buffers[(7-y)*8+x] = CRGB(buf[i*3+0], buf[i*3+1], buf[i*3+2]);
            i++;
        }
    }
    FastLED.show();
}

// 逐列填充
void Pixel::setScreen_Col(uint8_t *buf)
{
    uint8_t i = 0;

    // X轴
    for (uint8_t x = 0; x < RGB_LED_NUM_X; x++)
    {
        // Y轴
        for (uint8_t y = 0; y < RGB_LED_NUM_Y; y++)
        {
            // 每个像素点占3个byte
            color_buffers[(7-y)*8+x] = CRGB(buf[i*3+0], buf[i*3+1], buf[i*3+2]);
            i++;
        }
    }
    FastLED.show();
}
